package exttable.testversioning.testversioncreate;

import exttable.testversioning.tablemodel.AnalysisVersionModel;
import exttable.testversioning.testversionrunner.TestVersionView;
import exttable.view.ViewAnalysisParameter;

import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Iterator;
import java.util.Vector;

/**
 * Created by IntelliJ IDEA.
 * User: Admin
 * Date: Aug 6, 2010
 * Time: 5:49:48 PM
 * To change this template use File | Settings | File Templates.
 */
public class CreateAVController {
    CreateAVModel model;
    CreateAVView view;
    TestVersionView tvr;

    public CreateAVController(CreateAVModel model, CreateAVView view, TestVersionView tvr) {
        this.model = model;
        this.view = view;
        this.tvr = tvr;
        view.addMultiplyListener(new NextAction());
        view.addClearListener(new BackAction());
        view.addSaveAction(new SaveAction());
        view.addUpAction(new UpAction());
        view.addDownAction(new DownAction());
    }

    class NextAction implements ActionListener {
        @Override
        public void actionPerformed(ActionEvent e) {
            if (view.getUnitTable().getCellEditor() != null) {
                view.getUnitTable().getCellEditor().stopCellEditing();
            }

            model.getEditedTableData().clear();
            Vector<ViewAnalysisParameter> tableData = new Vector<ViewAnalysisParameter>();
            tableData.addAll(model.getOriginalTableData());
            for (Iterator<ViewAnalysisParameter> iterator = tableData.iterator(); iterator.hasNext();) {
                ViewAnalysisParameter res = iterator.next();
                if (!res.isPresent()) {
                    iterator.remove();
                }
            }
            for (int i = 0, tableDataSize = tableData.size(); i < tableDataSize; i++) {
                tableData.get(i).setOrder(i + 1);

            }
            model.getEditedTableData().addAll(tableData);
            view.getUnitTable().setModel(new AnalysisVersionModel(model.getEditedTableData(), false));
            view.getUnitTable().removeColumn(view.getUnitTable().getColumnModel().getColumn(1));
            view.getBackButton().setEnabled(true);
            view.getSaveData().setVisible(true);
            view.getNextButton().setEnabled(false);
        }
    }

    private class BackAction implements ActionListener {
        @Override
        public void actionPerformed(ActionEvent e) {
            view.getUnitTable().setModel(new AnalysisVersionModel(model.getOriginalTableData(), true));
            view.getUnitTable().getColumnModel().getColumn(2).setCellEditor(new DefaultCellEditor(view.getUnitsList()));
            view.getBackButton().setEnabled(false);
            view.getSaveData().setVisible(false);
            view.getNextButton().setEnabled(true);
            view.organiseLayout();
        }
    }

    private class SaveAction implements ActionListener {
        @Override
        public void actionPerformed(ActionEvent e) {
            try {
                if (model.getDataService().saveData(model.getEditedTableData(), model.getViewAnalysis(), model.getVersionAnalysis())) {
                    tvr.displayAnalysisVersionCreateList();
                    view.getFrame().dispose();
                }
            }
            catch (Exception ex) {
                ex.printStackTrace();
            }
        }
    }

    private class UpAction implements ActionListener {
        @Override
        public void actionPerformed(ActionEvent e) {
            int selectedRow = view.getUnitTable().getSelectedRow();
            if (selectedRow > 0) {
                ViewAnalysisParameter selectedVAP = model.getOriginalTableData().get(selectedRow);
                ViewAnalysisParameter upperVAR = model.getOriginalTableData().get(selectedRow - 1);
                model.getOriginalTableData().setElementAt(selectedVAP, selectedRow - 1);
                model.getOriginalTableData().setElementAt(upperVAR, selectedRow);
                view.getUnitTable().setRowSelectionInterval(selectedRow - 1, selectedRow - 1);
                view.getUnitTable().updateUI();
            }
        }
    }

    private class DownAction implements ActionListener {
        @Override
        public void actionPerformed(ActionEvent e) {
            int selectedRow = view.getUnitTable().getSelectedRow();
            int rowCount = view.getUnitTable().getRowCount();
            if (selectedRow < rowCount - 1) {
                ViewAnalysisParameter selectedVAP = model.getOriginalTableData().get(selectedRow);
                ViewAnalysisParameter upperVAR = model.getOriginalTableData().get(selectedRow + 1);
                model.getOriginalTableData().setElementAt(selectedVAP, selectedRow + 1);
                model.getOriginalTableData().setElementAt(upperVAR, selectedRow);
                view.getUnitTable().setRowSelectionInterval(selectedRow + 1, selectedRow + 1);
                view.getUnitTable().updateUI();
            }
        }
    }
}
